<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入单号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="销售订单id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleOrderId">
              <a-input v-model="model.saleOrderId" placeholder="请输入销售订单id" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="客户id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
              <a-input v-model="model.customerId" placeholder="请输入客户id" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="业务员id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="personId">
              <a-input v-model="model.personId" placeholder="请输入业务员id" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="收款日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiptDate">
              <a-input v-model="model.receiptDate" placeholder="请输入收款日期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="收款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalAmount">
              <a-input v-model="model.totalAmount" placeholder="请输入收款金额" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="审核人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approveBy">
              <a-input v-model="model.approveBy" placeholder="请输入审核人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approveTime">
              <a-input v-model="model.approveTime" placeholder="请输入审核时间" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="单据状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actstatus">
              <a-input-number v-model="model.actstatus" placeholder="请输入单据状态" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="驳回原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="refuseReason">
              <a-input v-model="model.refuseReason" placeholder="请输入驳回原因" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="支付状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentStatus">
              <a-input-number v-model="model.paymentStatus" placeholder="请输入支付状态" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def1">
              <a-input v-model="model.def1" placeholder="请输入自定义项1" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def2">
              <a-input v-model="model.def2" placeholder="请输入自定义项2" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项3" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def3">
              <a-input v-model="model.def3" placeholder="请输入自定义项3" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项4" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def4">
              <a-input v-model="model.def4" placeholder="请输入自定义项4" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项5" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def5">
              <a-input v-model="model.def5" placeholder="请输入自定义项5" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项6" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def6">
              <a-input v-model="model.def6" placeholder="请输入自定义项6" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项7" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def7">
              <a-input v-model="model.def7" placeholder="请输入自定义项7" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项8" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def8">
              <a-input v-model="model.def8" placeholder="请输入自定义项8" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项9" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="def9">
              <a-input v-model="model.def9" placeholder="请输入自定义项9" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="自定义项10" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="de10">
              <a-input v-model="model.de10" placeholder="请输入自定义项10" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="收款单子表" :key="refKeys[0]" :forceRender="true">
       <!-- <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="erpReceiptDetailTable.loading"
          :columns="erpReceiptDetailTable.columns"
          :dataSource="erpReceiptDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />-->
          <nbcio-editable-table
          :ref="refKeys[0]"
          v-on:beforeAdd="beforeAdd"
          :isAdd="isAdd"
          :loading="erpReceiptDetailTable.loading"
          :columns="erpReceiptDetailTable.columns"
          :dataSource="erpReceiptDetailTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          @valueChange="onValueChange"
          :rowSelection="true"
          :actionButton="true"
          >

          </nbcio-editable-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction,putAction } from '@/api/manage'
  //import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { NbcioTableModelMixin } from '@/mixins/NbcioTableModelMixin'
  import { GoodsModalMixin } from '../mixins/GoodsModalMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'ErpReceiptForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
         },
        //是否容许增加子表单行记录,false为不可以添加记录
        isAdd: false,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['erpReceiptDetail', ],
        tableKeys:['erpReceiptDetail', ],
        activeKey: 'erpReceiptDetail',
        // 收款单子表
        erpReceiptDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '订单明细ID',
              key: 'saleOrderDetailId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '金额',
              key: 'amount',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'description',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          rule: {
          // 编码规则校验 填写自己的编码规则
            orderCode: ruleBaseURL + 'ces_order_rule'
          },
          add: "/receipt/erpReceipt/add",
          edit: "/receipt/erpReceipt/edit",
          queryById: "/receipt/erpReceipt/queryById",
          erpReceiptDetail: {
            list: '/receipt/erpReceipt/queryErpReceiptDetailByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      getOrderCode() {
        putAction(this.url.rule.orderCode, this.model).then(res => {
          // 执行成功，获取返回的订单编号值，并赋到页面上
          if (res.success) {
            //赋值给自己定义的订单编号
            this.model.orderCode = res.result
          }
        })
      },
      addBefore(){
      this.getOrderCode();
        this.erpReceiptDetailTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.erpReceiptDetail.list, params, this.erpReceiptDetailTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          erpReceiptDetailList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      //子表新增前校验
     beforeAdd() {
       // if (this.checkSc() === false) {
       //     this.$message.error('请先选择仓库！')
       // }
     },
     //检查是否有仓库信息
     checkSc() {
       this.param.sc_id = this.model.scId;
       console.log("beforeAdd this.param.sc_id=",this.param.sc_id)
       if (this.param.sc_id === '' || this.param.sc_id === undefined) {
         this.isAdd = false
         return false
       }
       else {
         this.erpSaleOutDetailTable.columns[0].param = this.param;
         this.isAdd = true;
         return true
       }
     },

    }
  }
</script>

<style scoped>
</style>
